<template>
  <div>
    <van-row @click="openModal">
      <van-col span="6">
        <img :src="avatarSrc" alt="" />
      </van-col>
      <van-col span="15">{{ nickname }}</van-col>
      <van-col span="3">
        <van-icon name="arrow"></van-icon>
      </van-col>
    </van-row>
    <van-grid column-num="3" square>
      <van-grid-item icon="photo-o" text="文字" />
      <van-grid-item icon="photo-o" text="文字" />
      <van-grid-item icon="photo-o" text="文字" />
      <van-grid-item icon="photo-o" text="文字" />
    </van-grid>
    <transition name="van-fade">
      <!-- 登录盒子 -->
      <div class="login-modal" v-show="isShowModal">
        <!-- 这样来同等级定义盒子，防止冒泡 -->
        <div class="close-modal" @click="isShowModal = !isShowModal"></div>

        <van-form @submit="onSubmit" class="login-form">
          <van-field
            v-model="username"
            name="用户名"
            label="用户名"
            placeholder="用户名"
            :rules="[{ required: true, message: '请填写用户名' }]"
          />
          <van-field
            v-model="password"
            type="password"
            name="密码"
            label="密码"
            placeholder="密码"
            :rules="[{ required: true, message: '请填写密码' }]"
          />
          <div style="margin: 0.2rem">
            <van-button round block type="info" native-type="submit"
              >提交</van-button
            >
          </div>
        </van-form>
      </div>
    </transition>
  </div>
</template>

<script>
import { GoLogin } from "@/request/api";
export default {
  data() {
    return {
      avatarSrc: require("../assets/avatar.png"),
      username: "",
      password: "",
      isShowModal: false,
      nickname: "用户登录",
      my_avatar:
        "https://img.zcool.cn/community/01864559b54fa0a8012028a9c49861.jpg@1280w_1l_2o_100sh.jpg",
    };
  },
  methods: {
    onSubmit(values) {
      // console.log("submit", values);

      // 相当于values.用户名
      // 因为后面是中文名，所以不要用“.”的形式
      const username = values["用户名"];
      const pwd = values["密码"];
      GoLogin({ username, pwd }).then((res) => {
        if (res.errno == 0) {
          console.log(res.data.userInfo.avatar);
          // 登录成功了
          // 登录的业务逻辑
          // 1、提示框提示登录成功
          this.$toast.success("登录成功");
          // 2、把token保存到本地存储
          localStorage.setItem("token", res.data.token);
          localStorage.setItem("userInfo", JSON.stringify(res.data.userInfo));
          // 3、1s后关闭模态窗口
          setTimeout(() => {
            this.isShowModal = !this.isShowModal;
            // 4、把拿到的userInfo用户信息，填写到页面上
            this.nickname = res.data.userInfo.nickname;

            // 这个接口失效了
            // this.avatarSrc = res.data.userInfo.avatar;
            this.avatarSrc = this.my_avatar;
          }, 500);
        }
      });
    },
    openModal() {
      // 判断用户是否登录，如果登录直接就return，没有登录就isShowModal取反
      let token = localStorage.getItem("token");
      if (token) {
        return;
      }
      this.isShowModal = !this.isShowModal;
    },
  },
  created() {
    // 组件刚创建的时候需要判断用户有没有登录，获取localStorage有没有token值，
    // 如果有token，就表示登录，就应该填上用户信息
    const token = localStorage.getItem("token");
    if (token) {
      // 表示已经有登录状态，就要网页面上渲染用户信息
      const userInfo = JSON.parse(localStorage.getItem("userInfo"));
      this.nickname = userInfo.nickname;

      // 接口失效
      // this.avatarSrc = userInfo.avatar;
      this.avatarSrc = this.my_avatar;
    }
  },
};
</script>
<style lang="less" scoped>
.login-modal {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  z-index: 99;
  .close-modal {
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
  }
  .login-form {
    width: 90%;
    background-color: #fff;
    padding-bottom: 12px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
}

.van-row {
  padding: 16px 4%;
  background: #333;
  color: #fff;
  .van-col {
    line-height: 56px;
    font-size: 14.4px;
    img {
      width: 56px;
      border-radius: 50%;
      display: block;
    }
    &:last-child {
      text-align: right;
    }
  }
}
</style>